package com.hui.auth.dao;

import com.hui.auth.dao.sql.CommonSQLProvider;
import com.hui.auth.entity.AuthRole;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 角色
 * Created by LiaoYunHui on 2017/9/23/023.
 */
@Mapper
public interface AuthRoleDao {

    @InsertProvider(type = CommonSQLProvider.class, method = "insert")
    @Options(useGeneratedKeys = true)
    boolean insert(AuthRole role);

    boolean insertRoleMenus(@Param("roleId") Long roleId, @Param("menus") List<Integer> menus);

    boolean insertRolePerms(@Param("roleId") Long roleId, @Param("perms") List<Integer> perms);

    @UpdateProvider(type = CommonSQLProvider.class, method = "update")
    Integer update(AuthRole role);

    @Select("select * from auth_role")
    List<AuthRole> selectAll(Map params);

    /**
     * 根据角色rid查询角色信息
     */
    @Select("SELECT * FROM auth_role WHERE id = #{rid}")
    AuthRole select(long rid);

    /**
     * 删除角色
     */
    @Delete("delete from auth_role where id = #{id}")
    int delete(Integer id);

    /**
     * 删除角色和菜单的关联信息
     */
    @Delete("delete from auth_role_menu where rid = #{rid}")
    int deleteRoleMenu(Integer rid);

    /**
     * 删除角色和权限的关联信息
     */
    @Delete("delete from auth_role_permission where rid = #{rid}")
    int deleteRolePerm(Integer rid);

    /**
     * 根据用户uId查询用户角色rid
     */
    @Select("select r.id from auth_user_role ur left join auth_role r on ur.rid = r.id where ur.uid = #{uid}")
    List<Integer> selectIds(long uid);

    /**
     * 根据用户uId查询用户角色
     */
    @Select("select r.id,r.name from auth_user_role ur left join auth_role r on ur.rid = r.id where ur.uid = #{uid}")
    List<AuthRole> selectByUid(long uid);
}
